package com.cookie.framework.db;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.util.List;

/**
 * 项目名:    MeetProject
 * 包名:      com.cookie.framework.db
 * 文件名:    LitePalHelper
 * 创建者:    cookie
 * 创建时间:   2020/1/14 19:19
 * 描述:      本地数据库帮助类
 */

public class LitePalHelper {

    private static volatile LitePalHelper instance = null;

    private LitePalHelper() {
    }

    public static LitePalHelper getInstance() {
        if (instance == null) {
            synchronized (LitePalHelper.class) {
                if (instance == null) {
                    instance = new LitePalHelper();
                }
            }
        }
        return instance;
    }

    //保存新朋友
    public void saveNewFriend(String msg, String userId) {
        NewFriend friend = new NewFriend();
        friend.setMsg(msg);
        friend.setUserId(userId);
        friend.setSaveTime(System.currentTimeMillis());
        friend.setIsAgree(-1);
        baseSave(friend);
    }

    //保存通话记录
    public void saveCallRecord(String userId, int mediaType, int callStatus) {
        CallRecord callRecord = new CallRecord();
        callRecord.setUserId(userId);
        callRecord.setMediaType(mediaType);
        callRecord.setCallStatus(callStatus);
        callRecord.setCallTime(System.currentTimeMillis());
        baseSave(callRecord);
    }

    //查询新朋友
    public List<NewFriend> queryNewFriend() {
        return (List<NewFriend>) baseQuery(NewFriend.class);
    }

    //查询通话记录
    public List<CallRecord> queryCallRecord() {
        return (List<CallRecord>) baseQuery(CallRecord.class);
    }

    //更新新朋友的数据库状态
    public void updateNewFriend(String userId, int agree) {
        NewFriend friend = new NewFriend();
        friend.setIsAgree(agree);
        friend.updateAll("userId=?", userId);
    }

    //保存的基类
    private void baseSave(LitePalSupport support) {
        support.save();
    }

    //查询的基类
    private List<? extends LitePalSupport> baseQuery(Class clz) {
        return LitePal.findAll(clz);
    }
}
